<html>

<head>
  <title>webrtc-min</title>
  <meta charset="UTF-8" />
  <script>
    let createPc;
    const offerOptions = {offerToReceiveAudio: 1};
    document.onreadystatechange = () => {
      if (document.readyState === 'complete') {
        createPc = () => {
          return new Promise(function(resolve,reject){
            let div = document.createElement('div');
            document.body.appendChild(div);
            div.write = (text) => div.innerHTML += text;
            let config = {
              "iceServers": [
                { "urls": ["stun:b.7mo.org:3478"] }
              ],
              "iceTransportPolicy": "all",
              "iceCandidatePoolSize": "0"
            };
            let start = Date.now();
            let pc = new RTCPeerConnection(config);
            pc.errorCount = 0;
            pc.createOffer(offerOptions).then(
              desc => pc.setLocalDescription(desc).then(() => {
                console.log(pc.localDescription);
                let cost = Date.now() - start;
                div.write('<p><b>success ice ' + cost + '</b></p>');
                div.write(JSON.stringify(pc.localDescription))
              })
            );

            pc.onicecandidate = (e) => {
              let ice = e.candidate;
              console.log('ice======', e.candidate);
              if (ice != null) {
                start = start === null?Date.now():start;
                if (ice.candidate) {
                  console.log('ice', pc.localDescription);
                  div.write('<p><b>ice ' + ice.candidate + '</b></p>')
                }
              } else {
                let cost = Date.now() - start;
                start = null;
                console.log('ice finished ' + cost);
                let result = 'success';
                if(pc.localDescription.sdp.indexOf('c=IN IP4 0.0.0.0') > 0){
                  result = 'failed';
                }
                if(result != pc.iceResult){
                  console.log('setIceResult',result);
                  pc.iceResult = result;
                  resolve(pc);
                }
                div.write('<p><b>' + 'ice finished ' + cost + ' ,ice result ' + result + ' ,errorCount ' + pc.errorCount + '</b></p>');
              }
            };

            pc.onicecandidateerror = (e) => {
              console.error('********* ice error: %s msg: %s %o', e.errorCode, e.errorText, window.e=e);
              pc.errorCount = pc.errorCount + 1;
            };

            pc.onicegatheringstatechange = (e) => {
              console.info('---------- ice gather state: ',e.target.iceGatheringState,e);
            };

            // WebRTC: ICE failed, add a TURN server and see about:webrtc for more details
            // 20200703 网络防火墙原因，在建立Offer与Answer的中途会有如上报错，暂无法捕获错误，经检查发现pc.iceConnectionState状态为failed
            pc.oniceconnectionstatechange = (e) => {
              if('failed' === e.target.iceConnectionState){
                e.target.errorCount = e.target.errorCount + 1;
                console.info('||||||||||| ice connect state %s,errorCount %s %o',e.target.iceConnectionState,e.target.errorCount,e);
              }else{
                console.info('||||||||||| ice connect state: ',e.target.iceConnectionState,e);
              }
            };

            pc.ondatachannel = (e) => {

            };

          })
        }
      }
    };


  </script>
</head>

<body></body>

</html>